package com.zsgeek.wisdchain.qutu.view.me;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zsgeek.wisdchain.qutu.BaseActivity;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.base.customControl.EmptyRecyclerView;
import com.zsgeek.wisdchain.qutu.base.customControl.LinearLayoutColorDivider;
import com.zsgeek.wisdchain.qutu.base.http.JsonRequest;
import com.zsgeek.wisdchain.qutu.base.http.JsonRequestCallBack;
import com.zsgeek.wisdchain.qutu.base.model.GetCommentsListModel;
import com.zsgeek.wisdchain.qutu.tools.TextUtils;
import com.zsgeek.wisdchain.qutu.tools.ToastHelper;
import com.zsgeek.wisdchain.qutu.tools.UrlPathHelper;
import com.zsgeek.wisdchain.qutu.view.MainActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的关注
 */
public class MyConcernActivity extends BaseActivity {
    @BindView(R.id.list_date_emptyView)
    View list_date_emptyView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    EmptyRecyclerView emptyRecyclerView;

    //    private String address = "1Dz1gWJ3Ukvpxah43zZyz9HgRCHqoSs42D";
    private int pageIndex = 1;
    private List<GetCommentsListModel.DataBean> dataListBeans = new ArrayList<>();
    private int type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_concern;
    }

    @Override
    protected void initView() {
        emptyRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        emptyRecyclerView.addItemDecoration(new LinearLayoutColorDivider(this.getResources(),
                R.color.color_EAEAEA, R.dimen.one, LinearLayoutManager.VERTICAL));
        emptyRecyclerView.setEmptyView(list_date_emptyView);
        emptyRecyclerView.setAdapter(mAdapter);
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
////                startActivityForResult(new Intent(ManagementWalletActivity.this, WalletDetailsActivity.class).putExtra("data", dataListBeans.get(position)), 0x100);
//            }
//        });
        initRefreshLayout();
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type", -1);
        if (type != -1) {
            String titleName = null;
            if (type == 0) {
                titleName = "我的关注";
            }
            setTitle(titleName);
            refreshLayout.autoRefresh();
        }
    }


    /**
     * type 1 评论 2 点赞 3 阅读
     */
    private void getCommentsList() {
        if (TextUtils.isEmpty(MainActivity.address)) {
            return;
        }
        new JsonRequest(new JsonRequestCallBack() {
            @Override
            public void showResult(Object object, String taskId) {
                if (object instanceof GetCommentsListModel) {
                    GetCommentsListModel dataModel = (GetCommentsListModel) object;
                    stopRefresh();
                    if (dataModel.getCode() == 2000) {
                        List<GetCommentsListModel.DataBean> dataBeans = dataModel.getData();
                        if (pageIndex == 1) {
                            dataListBeans.clear();
                        }
                        if (!TextUtils.isEmpty(dataBeans)) {
                            dataListBeans.addAll(dataBeans);
                        }
                        mAdapter.notifyDataSetChanged();
                        if (dataModel.getPageQuery() != null && dataModel.getPageQuery().getTotalPage() <= pageIndex) {
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    } else {
                        ToastHelper.showToast(dataModel.getMessage());
                    }
                }
            }

            @Override
            public void showError(String errorMsg, String taskId) {
                ToastHelper.showToast(errorMsg);
                stopRefresh();
            }
        }).postJson(UrlPathHelper.getAdver() + "user/getCommentsList?addr=" + MainActivity.address + "&pageSize=5&pageIndex=" + pageIndex, true, null, GetCommentsListModel.class);
    }

    private BaseQuickAdapter<GetCommentsListModel.DataBean, BaseViewHolder> mAdapter =
            new BaseQuickAdapter<GetCommentsListModel.DataBean, BaseViewHolder>(R.layout.fragment_message_item, dataListBeans) {
                @Override
                protected void convert(BaseViewHolder baseViewHolder, final GetCommentsListModel.DataBean dataBean) {
                    baseViewHolder.setText(R.id.title_tv, dataBean.getNickName());
                    baseViewHolder.setText(R.id.content_tv, dataBean.getInfo());
                    baseViewHolder.setText(R.id.create_time_tv, dataBean.getCreateTime());
//                    ImageUtils.loadImage(getActivity(), dataBean.getByAddr(), baseViewHolder.getView(R.id.avatar_iv));
                }
            };

    private void initRefreshLayout() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                pageIndex = 1;
                getCommentsList();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageIndex++;
                getCommentsList();
            }
        });
    }

    private void stopRefresh() {
        if (pageIndex == 1) {
            refreshLayout.finishRefresh();
            refreshLayout.setEnableLoadMore(true);
        } else {
            refreshLayout.finishLoadMore();
            refreshLayout.setEnableRefresh(true);
        }
    }
}
